package com.tjs.lucky.infrastructure.repository;

import com.tjs.lucky.common.GrantState;
import com.tjs.lucky.common.Ids;
import com.tjs.lucky.common.MQState;
import com.tjs.lucky.domain.activity.model.vo.DisAwardInvoiceVO;
import com.tjs.lucky.domain.activity.model.vo.UserTakeActivityVO;
import com.tjs.lucky.domain.activity.repository.IUserTakeActivityRepository;
import com.tjs.lucky.domain.strategy.model.vo.DrawAwardVO;
import com.tjs.lucky.domain.support.ids.DefindeIdGenerator;
import com.tjs.lucky.infrastructure.mapper.IUserStrategyExportMapper;
import com.tjs.lucky.infrastructure.mapper.IUserTakeActivityMapper;
import com.tjs.lucky.infrastructure.po.UserStrategyExport;
import com.tjs.lucky.infrastructure.po.UserTakeActivity;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: 田佳澍
 * @Description: 用户参与活动仓储
 * @DateTime: 2023/5/25 16:22
 **/
@Repository
public class UserTakeActivityRepository implements IUserTakeActivityRepository {

    @Resource
    private IUserTakeActivityMapper userTakeActivityMapper;

    @Resource
    private IUserStrategyExportMapper userStrategyExportMapper;

    @Resource
    private Map<Ids, DefindeIdGenerator> idGeneratorMap;

    @Override
    public void takeActivity(Long activityId, String activityName, Long strategyId, String uId,Integer userTakeLeftCount, Date takeDate, Long takeId, Integer activityPartLeftCount) {
        UserTakeActivity userTakeActivity = new UserTakeActivity();
        userTakeActivity.setUId(uId);
        userTakeActivity.setTakeId(takeId);
        userTakeActivity.setActivityId(activityId);
        userTakeActivity.setActivityName(activityName);
        userTakeActivity.setTakeDate(takeDate);
        userTakeActivity.setActivityPartLeftCount(activityPartLeftCount);
        userTakeActivity.setStrategyId(strategyId);
        String uuid = uId + "_" + activityId;
        userTakeActivity.setUuid(uuid);
        userTakeActivity.setUserTakeLeftCount(userTakeLeftCount);

        userTakeActivityMapper.insert(userTakeActivity);
    }

    @Override
    public void saveUserStrategyExport(DrawAwardVO drawAwardVO,Long activityId) {
        UserStrategyExport userStrategyExport = new UserStrategyExport();
        userStrategyExport.setUId(drawAwardVO.getUId());
        userStrategyExport.setActivityId(activityId);
        userStrategyExport.setOrderId(idGeneratorMap.get(Ids.SnowFlake).nextId());
        userStrategyExport.setStrategyId(drawAwardVO.getStrategyId());
        userStrategyExport.setStrategyMode(drawAwardVO.getStrategyMode());
        userStrategyExport.setGrantType(drawAwardVO.getGrantType());
        userStrategyExport.setGrantDate(drawAwardVO.getGrantDate());
        userStrategyExport.setGrantState(GrantState.INIT.getCode());
        userStrategyExport.setAwardId(drawAwardVO.getAwardId());
        userStrategyExport.setAwardType(drawAwardVO.getAwardType());
        userStrategyExport.setAwardName(drawAwardVO.getAwardName());
        userStrategyExport.setAwardContent(drawAwardVO.getAwardContent());
        userStrategyExport.setMqState(MQState.INIT.getCode());

        userStrategyExportMapper.insert(userStrategyExport);
    }

    @Override
    public boolean deductUserTakeCount(Long activityId, String uId) {
        return userTakeActivityMapper.deductUserTakeCount(uId,activityId);
    }

    @Override
    public UserTakeActivityVO queryNoConsumedTakeActivityOrder(Long activityId, String uId) {

        UserTakeActivity userTakeActivity = new UserTakeActivity();
        userTakeActivity.setUId(uId);
        userTakeActivity.setActivityId(activityId);
        UserTakeActivity noConsumedTakeActivityOrder = userTakeActivityMapper.queryNoConsumedTakeActivityOrder(userTakeActivity);

        // 未查询到符合的领取单，直接返回 NULL
        if (null == noConsumedTakeActivityOrder) {
            return null;
        }

        UserTakeActivityVO userTakeActivityVO = new UserTakeActivityVO();
        userTakeActivityVO.setActivityId(noConsumedTakeActivityOrder.getActivityId());
        userTakeActivityVO.setTakeId(noConsumedTakeActivityOrder.getTakeId());
        userTakeActivityVO.setStrategyId(noConsumedTakeActivityOrder.getStrategyId());
        userTakeActivityVO.setActivityLeftPartCount(noConsumedTakeActivityOrder.getActivityPartLeftCount());
        userTakeActivityVO.setUserTakeLeftCount(noConsumedTakeActivityOrder.getUserTakeLeftCount());
        return userTakeActivityVO;
    }

    @Override
    public void updateInvoiceMqState(String uId, Long orderId, Integer mqState) {
        UserStrategyExport userStrategyExport = new UserStrategyExport();
        userStrategyExport.setUId(uId);
        userStrategyExport.setOrderId(orderId);
        userStrategyExport.setMqState(mqState);
        userStrategyExportMapper.updateInvoiceMqState(userStrategyExport);
    }

    @Override
    public List<DisAwardInvoiceVO> scanInvoiceMqState() {
        // 查询发送MQ失败和超时30分钟，未发送MQ的数据
        List<UserStrategyExport> userStrategyExportList = userStrategyExportMapper.scanInvoiceMqState();
        // 转换对象
        return userStrategyExportList.stream()
                .map(userStrategyExport -> {
                    DisAwardInvoiceVO disAwardInvoiceVO = new DisAwardInvoiceVO();
                    disAwardInvoiceVO.setUId(userStrategyExport.getUId());
                    disAwardInvoiceVO.setOrderId(userStrategyExport.getOrderId());
                    disAwardInvoiceVO.setAwardId(userStrategyExport.getAwardId());
                    disAwardInvoiceVO.setAwardType(userStrategyExport.getAwardType());
                    disAwardInvoiceVO.setAwardName(userStrategyExport.getAwardName());
                    disAwardInvoiceVO.setAwardContent(userStrategyExport.getAwardContent());
                    return disAwardInvoiceVO;
                }).collect(Collectors.toList());
    }

    @Override
    public void updateGrantState(String uId, Long orderId, Integer grantState) {
        UserStrategyExport userStrategyExport = new UserStrategyExport();
        userStrategyExport.setUId(uId);
        userStrategyExport.setOrderId(orderId);
        userStrategyExport.setGrantState(grantState);
        userStrategyExportMapper.updateGrantState(userStrategyExport);
    }

}
